# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
"""
Model validation metrics
"""

import math
import warnings
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
import torch


def fitness(x):
    # 用不同的权重的和作为衡量的标准
    # Model fitness as a weighted combination of metrics
    w = [0.0, 0.0, 0.1, 0.9]  # weights for [P, R, mAP@0.5, mAP@0.5:0.95]
    return (x[:, :4] * w).sum(1)


def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=()):
    """ Compute the average precision, given the recall and precision curves.
    Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
    # Arguments
        tp:  True positives (nparray, nx1 or nx10).
        conf:  Objectness value from 0-1 (nparray).
        pred_cls:  Predicted object classes (nparray).
        target_cls:  True object classes (nparray).
        plot:  Plot precision-recall curve at mAP@0.5
        save_dir:  Plot save directory
    # Returns
        The average precision as computed in py-faster-rcnn.
    """

    # Sort by objectness  &np_argsort_test
    i = np.argsort(-conf)  # 按照置信度降序排序
    tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]

    # Find unique classes
    unique_classes = np.unique(target_cls)  # 真实情况是有多少个类别即去重
    nc = unique_classes.shape[0]  # number of classes, number of detections label中总共发现了多少个类别
    # 生成召回率曲线，并计算每个类别的平均精度 AP:average precise
    # Create Precision-Recall curve and compute AP for each class
    px, py = np.linspace(0, 1, 1000), []  # for plotting
    ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000))
    for ci, c in enumerate(unique_classes):
        i = pred_cls == c
        n_l = (target_cls == c).sum()  # number of labels 某个类别下多少个物体
        n_p = i.sum()  # number of predictions  # 统计某个类别有几个预测正确了

        if n_p == 0 or n_l == 0:
            continue
        else:
            # Accumulate FPs and TPs &np_cumsum_test
            fpc = (1 - tp[i]).cumsum(0)  # 统计mAP0.5~0.95每列的负样本个数
            tpc = tp[i].cumsum(0)  # 正样本的个数

            # Recall
            recall = tpc / (n_l + 1e-16)  # recall curve 计算召回率
            # 召回率插值 为画图做准备 为啥是负值
            r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0)  # negative x, xp because xp decreases

            # Precision 计算精度
            precision = tpc / (tpc + fpc)  # precision curve
            # 精度插值
            p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1)  # p at pr_score

            # AP from recall-precision curve
            # 遍历mAP0.5~0.95 并计算ap
            for j in range(tp.shape[1]):
                # ap[ci, j]:ci类别的mAP j mpre平均精度，mrec平均召回率
                ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j])
                if plot and j == 0:
                    # 用插值表示的平均精度曲线 用于画图
                    py.append(np.interp(px, mrec, mpre))  # precision at mAP@0.5
    #
    # Compute F1 (harmonic mean of precision and recall)
    # F1:精度和召回率的调和平均值
    f1 = 2 * p * r / (p + r + 1e-16)
    if plot:
        # 画精度召回率曲线
        plot_pr_curve(px, py, ap, Path(save_dir) / 'PR_curve.png', names)
        # 精度，召回率调和平均值曲线
        plot_mc_curve(px, f1, Path(save_dir) / 'F1_curve.png', names, ylabel='F1')
        # 精度曲线
        plot_mc_curve(px, p, Path(save_dir) / 'P_curve.png', names, ylabel='Precision')
        # 召回率曲线
        plot_mc_curve(px, r, Path(save_dir) / 'R_curve.png', names, ylabel='Recall')
    # 调和平均值最大的索引
    i = f1.mean(0).argmax()  # max F1 index
    return p[:, i], r[:, i], ap, f1[:, i], unique_classes.astype('int32')


def compute_ap(recall, precision):
    """ Compute the average precision, given the recall and precision curves
    # Arguments
        recall:    The recall curve (list)
        precision: The precision curve (list)
    # Returns
        Average precision, precision curve, recall curve
    """

    # Append sentinel values to beginning and end
    # 添加起始的召回率，精度的值
    mrec = np.concatenate(([0.0], recall, [1.0]))
    mpre = np.concatenate(([1.0], precision, [0.0]))

    # Compute the precision envelope
    # np.flip 点击去看例子可以发现是上下翻转mpre的意思
    # &np_maximum_test  为啥要这样操作？
    mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))

    # Integrate area under curve
    method = 'interp'  # methods: 'continuous', 'interp'
    if method == 'interp':  # 这不写死了还else啥
        x = np.linspace(0, 1, 101)  # 101-point interp (COCO)
        # np.trapz 用梯形面积的平均值作为ap
        # https://blog.csdn.net/jacke121/article/details/118811618
        ap = np.trapz(np.interp(x, mrec, mpre), x)  # integrate
    else:  # 'continuous'
        i = np.where(mrec[1:] != mrec[:-1])[0]  # points where x axis (recall) changes
        ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])  # area under curve

    return ap, mpre, mrec


class ConfusionMatrix:
    # Updated version of https://github.com/kaanakan/object_detection_confusion_matrix
    def __init__(self, nc, conf=0.25, iou_thres=0.45):

        # zeros：第一个参数为预测类别，第二个为目标类别 1：是代表背景
        self.matrix = np.zeros((nc + 1, nc + 1))

        self.nc = nc  # number of classes
        self.conf = conf
        self.iou_thres = iou_thres

    def process_batch(self, detections, labels):
        """
        Return intersection-over-union (Jaccard index) of boxes.
        Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
        Arguments:
            detections (Array[N, 6]), x1, y1, x2, y2, conf, class
            labels (Array[M, 5]), class, x1, y1, x2, y2
        Returns:
            None, updates confusion matrix accordingly
        """
        # 选出置信度大于给定置信度的预测物体
        detections = detections[detections[:, 4] > self.conf]
        # 目标类别
        gt_classes = labels[:, 0].int()
        # 预测类别
        detection_classes = detections[:, 5].int()
        # 计算两者交并比
        iou = box_iou(labels[:, 1:], detections[:, :4])
        # 返回交并比大于阈值的坐标
        x = torch.where(iou > self.iou_thres)
        if x[0].shape[0]:
            # 将符合条件的坐标值和IOU值选出来 matches:[(x,y,conf),]
            matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy()
            if x[0].shape[0] > 1:
                matches = matches[matches[:, 2].argsort()[::-1]]
                # 注意这里边返回的是索引即最终的结果会以1那一列升序排列
                matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
                # 这个还是让其按照置信度降序排列，那之前的那个是不是有点问题呢？&matches_test
                matches = matches[matches[:, 2].argsort()[::-1]]
                matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
        else:
            matches = np.zeros((0, 3))

        n = matches.shape[0] > 0  # 是否匹配到了
        # m0:label的物体，m1：预测的物体 &np_T_transpose_test
        m0, m1, _ = matches.transpose().astype(np.int16)  # transpose 转置的意思，直接写个T就行
        for i, gc in enumerate(gt_classes):  # 目标类别
            j = m0 == i  # 找到m0和i本质上是同一个label的索引
            if n and sum(j) == 1:
                # 其实就是统计每个预测类别和对应的目标类别分别对应那个值，找到了就将其加1 这也就是这个函数主要做的工作
                #    目 标 类 别(81)
                # 预  1 1...
                # 测  3 2...
                # 类  ...
                # 别(也81)
                self.matrix[detection_classes[m1[j]], gc] += 1  # correct 预测正确同时也是确实正确
            else:
                self.matrix[self.nc, gc] += 1  # background FP 我推测这个fp 为False Positive 预测错误，但是是正确

        if n:
            for i, dc in enumerate(detection_classes):
                if not any(m1 == i):
                    self.matrix[dc, self.nc] += 1  # background FN False Negitive即预测错误，同时也是错误的

    def matrix(self):
        return self.matrix

    def plot(self, normalize=True, save_dir='', names=()):
        try:
            import seaborn as sn
            # 标准化列
            array = self.matrix / ((self.matrix.sum(0).reshape(1, -1) + 1E-6) if normalize else 1)  # normalize columns
            array[array < 0.005] = np.nan  # don't annotate (would appear as 0.00)
            # 设置画布
            fig = plt.figure(figsize=(12, 9), tight_layout=True)
            # 设置label字体大小
            sn.set(font_scale=1.0 if self.nc < 50 else 0.8)  # for label size
            labels = (0 < len(names) < 99) and len(names) == self.nc  # apply names to ticklabels
            with warnings.catch_warnings():
                warnings.simplefilter('ignore')  # suppress empty matrix RuntimeWarning: All-NaN slice encountered
                # sn和fig是咋关联起来的？ &sn_plt_test
                sn.heatmap(array, annot=self.nc < 30, annot_kws={"size": 8}, cmap='Blues', fmt='.2f', square=True,
                           xticklabels=names + ['background FP'] if labels else "auto",
                           yticklabels=names + ['background FN'] if labels else "auto").set_facecolor((1, 1, 1))
            fig.axes[0].set_xlabel('True')
            fig.axes[0].set_ylabel('Predicted')
            fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250)
            plt.close()
        except Exception as e:
            print(f'WARNING: ConfusionMatrix plot failure: {e}')

    def print(self):
        for i in range(self.nc + 1):
            print(' '.join(map(str, self.matrix[i])))

# # 第一个box1为预测框，第二个为target框
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7):
    # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
    box2 = box2.T

    # Get the coordinates of bounding boxes
    if x1y1x2y2:  # x1, y1, x2, y2 = box1
        b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
        b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
    else:  # transform from xywh to xyxy
        # x1 = x-w/2 x2= x+w/2
        b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
        b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
        b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
        b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2

    # anchor1_diagonal = (b1_x2-b1_x1)**2 + (b1_y2-b1_y1)**2
    # anchor2_diagonal = (b2_x2-b2_x1)**2 + (b2_y2-b2_y1)**2
    # anchor_ratio = anchor1_diagonal/anchor2_diagonal
    # Intersection area
    # w = x2-x1 h = y2-y1 s = w*h
    inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
            (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)

    # Union Area 那要是没相交呢 答案是不可能的，因为有个阈值，把不相交的过滤了 找找？
    w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
    w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
    union = w1 * h1 + w2 * h2 - inter + eps

    left_up_x = torch.min(b1_x1, b2_x1)
    left_up_y = torch.min(b1_y1, b2_y1)
    right_down_x = torch.max(b1_x2, b2_x2)
    right_down_y = torch.max(b1_y2, b2_y2)
    warp_x_2 = (right_down_x-left_up_x)**2
    warp_y_2 = (right_down_y-left_up_y)**2

    wrap_diagonal = warp_x_2 + warp_y_2
    anchor1_diagonal = w1**2 + h1**2
    anchor2_diagonal = w2**2 + h2**2
    # anchor_ratio = anchor1_diagonal / anchor2_diagonal + eps

    iou = inter / union
    if GIoU or DIoU or CIoU:
        cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1)  # convex (smallest enclosing box) width
        ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1)  # convex height
        if CIoU or DIoU:  # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
            c2 = cw ** 2 + ch ** 2 + eps  # convex diagonal squared
            rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
                    (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4  # center distance squared
            if DIoU:
                return iou - rho2 / c2  # DIoU
            elif CIoU:  # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
                v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
                # v1 = (4 / math.pi ** 2) * torch.pow(torch.atan(anchor1_diagonal / wrap_diagonal) - torch.atan(anchor1_diagonal / anchor2_diagonal), 2)
                # v1 = (4 / math.pi ** 2) * torch.pow(torch.atan(anchor2_diagonal / wrap_diagonal) - torch.atan(anchor1_diagonal / anchor2_diagonal), 2)
                # 效果和上边的差不多
                # 第三个：在第二个的基础上
                # v1 = (4 / math.pi ** 2) * torch.pow(torch.atan(anchor1_diagonal / anchor2_diagonal), 2)
                # 第五种
                # v1 = (4 / math.pi ** 2) * torch.pow(torch.sigmoid(anchor1_diagonal / wrap_diagonal) - torch.sigmoid(anchor1_diagonal / anchor2_diagonal), 2)
                # 第六种
                # v1 = torch.pow(torch.sigmoid(anchor1_diagonal / wrap_diagonal) - torch.sigmoid(anchor1_diagonal / anchor2_diagonal), 2)
                # 第九种
                v1 = (4 / math.pi ** 2) * torch.pow(torch.tanh(anchor1_diagonal / wrap_diagonal) - torch.tanh(anchor1_diagonal / anchor2_diagonal), 2)
                # 第七种
                # v1 = torch.pow(torch.sigmoid(anchor1_diagonal / anchor2_diagonal), 2)

                # 加是不行的，必须是减
                # v1 = (4 / math.pi ** 2) * torch.pow(torch.atan(anchor1_diagonal / wrap_diagonal) + torch.atan(anchor1_diagonal / anchor2_diagonal), 2)
                # print("CIOU调用到了eps", eps)
                with torch.no_grad():
                    # 第一个
                    # anchor_ratio = anchor1_diagonal / anchor2_diagonal + anchor1_diagonal / wrap_diagonal + eps
                    # 第二个
                    anchor_ratio = anchor1_diagonal / anchor2_diagonal - anchor1_diagonal / wrap_diagonal + eps
                    # 第八种 和第三种结合
                    # anchor_ratio = anchor1_diagonal / anchor2_diagonal + eps
                    # print(anchor_ratio)
                    alpha2 = anchor_ratio / (anchor_ratio - iou + (1 + eps))
                    # alpha = v / (v - iou + (1 + eps))
                    v_a = v + anchor_ratio
                    alpha = v / (v - iou + (1 + eps))
                    alpha1 = v1 / (v1 - iou + (1 + eps))
                # print(v * alpha > 1)
                # print(v * alpha)
                # print(alpha*anchor_ratio > 1)
                # print(alpha*anchor_ratio)
                # print('*'*50)

                # print("-"*20)
                # print(alpha*anchor_ratio)
                # return iou - (rho2 / c2 + v * alpha)  # CIoU
                # if rho2 / c2 + alpha*anchor_ratio > eps:
                #     return iou - (rho2 / c2 + alpha * v)  # CIoU
                # else:
                #     print("*"*100)
                # return iou - (rho2 / c2 + alpha * v)
                # return iou - (rho2 / c2 + anchor_ratio)
                # return iou - (rho2 / c2 + alpha * v_a)
                # return iou - (rho2 / c2 + alpha * v + alpha2*anchor_ratio)
                # 第四个
                # return iou - (rho2 / c2 + alpha * v * anchor_ratio)  # CIoU
                return iou - (rho2 / c2 + alpha1*v1*anchor_ratio)
        else:  # GIoU https://arxiv.org/pdf/1902.09630.pdf
            c_area = cw * ch + eps  # convex area
            return iou - (c_area - union) / c_area  # GIoU
    else:
        return iou  # IoU


def box_iou(box1, box2):
    # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
    """
    Return intersection-over-union (Jaccard index) of boxes.
    Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
    Arguments:
        box1 (Tensor[N, 4])
        box2 (Tensor[M, 4])
    Returns:  返回的是box1中的每个盒子和box2中的每个盒子的交并比，box1盒子个数为横坐标，box2盒子个数为纵坐标
        iou (Tensor[N, M]): the NxM matrix containing the pairwise
            IoU values for every element in boxes1 and boxes2
    """

    def box_area(box):
        # box = 4xn  计算盒子面积
        return (box[2] - box[0]) * (box[3] - box[1])

    area1 = box_area(box1.T)
    area2 = box_area(box2.T)

    # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
    inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2)
    return inter / (area1[:, None] + area2 - inter)  # iou = inter / (area1 + area2 - inter)


def bbox_ioa(box1, box2, eps=1E-7):
    """ Returns the intersection over box2 area given box1, box2. Boxes are x1y1x2y2
    返回的是box1和box2的交集和box2的比值
    box1:       np.array of shape(4)
    box2:       np.array of shape(nx4)
    returns:    np.array of shape(n)
    """

    box2 = box2.transpose()

    # Get the coordinates of bounding boxes
    b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
    b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]

    # Intersection area 交并比，求的是面积，所以下式子s = w*h 为交集面积
    inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * \
                 (np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1)).clip(0)

    # box2 area 并集面积 eps为误差
    box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + eps

    # Intersection over box2 area
    return inter_area / box2_area


def wh_iou(wh1, wh2):
    # Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
    wh1 = wh1[:, None]  # [N,1,2]
    wh2 = wh2[None]  # [1,M,2]
    inter = torch.min(wh1, wh2).prod(2)  # [N,M]
    return inter / (wh1.prod(2) + wh2.prod(2) - inter)  # iou = inter / (area1 + area2 - inter)


# Plots ----------------------------------------------------------------------------------------------------------------

def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()):
    # Precision-recall curve
    fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
    py = np.stack(py, axis=1)

    if 0 < len(names) < 21:  # display per-class legend if < 21 classes 显示图例
        for i, y in enumerate(py.T):
            ax.plot(px, y, linewidth=1, label=f'{names[i]} {ap[i, 0]:.3f}')  # plot(recall, precision)
    else:  # 画召回率，精度线
        ax.plot(px, py, linewidth=1, color='grey')  # plot(recall, precision)

    ax.plot(px, py.mean(1), linewidth=3, color='blue', label='all classes %.3f mAP@0.5' % ap[:, 0].mean())
    ax.set_xlabel('Recall')
    ax.set_ylabel('Precision')
    ax.set_xlim(0, 1)  # 设置x轴的起始和终止值
    ax.set_ylim(0, 1)
    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
    fig.savefig(Path(save_dir), dpi=250)
    plt.close()


def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence', ylabel='Metric'):
    # Metric-confidence curve
    fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)

    if 0 < len(names) < 21:  # display per-class legend if < 21 classes
        for i, y in enumerate(py):
            ax.plot(px, y, linewidth=1, label=f'{names[i]}')  # plot(confidence, metric)
    else:
        ax.plot(px, py.T, linewidth=1, color='grey')  # plot(confidence, metric)

    y = py.mean(0)
    ax.plot(px, y, linewidth=3, color='blue', label=f'all classes {y.max():.2f} at {px[y.argmax()]:.3f}')
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
    fig.savefig(Path(save_dir), dpi=250)
    plt.close()
